package com.mdd.admin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IFastTypeService;
import com.mdd.admin.validate.FastTypeCreateValidate;
import com.mdd.admin.validate.FastTypeUpdateValidate;
import com.mdd.admin.validate.FastTypeSearchValidate;
import com.mdd.admin.vo.FastTypeListedVo;
import com.mdd.admin.vo.FastTypeDetailVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.FastType;
import com.mdd.common.mapper.FastTypeMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 快递分类配置实现类
 * @author DEREK
 */
@Service
public class FastTypeServiceImpl implements IFastTypeService {
        
    @Resource
    FastTypeMapper fastTypeMapper;

    /**
     * 快递分类配置列表
     *
     * @author DEREK
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<FastTypeListedVo>
     */
    @Override
    public PageResult<FastTypeListedVo> list(PageValidate pageValidate, FastTypeSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<FastType> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        fastTypeMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:relatedId@related_id:int",
            "like:name:str",
            "=:icon:str",
            "=:price:str",
            "=:remark:str",
            "=:extend:str",
        });

        IPage<FastType> iPage = fastTypeMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<FastTypeListedVo> list = new LinkedList<>();
        for(FastType item : iPage.getRecords()) {
            FastTypeListedVo vo = new FastTypeListedVo();
            BeanUtils.copyProperties(item, vo);
            if(null != item.getCreateTime()) {
            	vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            }
            if(null != item.getUpdateTime()) {
            	vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            }
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 快递分类配置详情
     *
     * @author DEREK
     * @param id 主键参数
     * @return FastType
     */
    @Override
    public FastTypeDetailVo detail(Integer id) {
        FastType model = fastTypeMapper.selectOne(
                new QueryWrapper<FastType>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        FastTypeDetailVo vo = new FastTypeDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 快递分类配置新增
     *
     * @author DEREK
     * @param createValidate 参数
     */
    @Override
    public void add(FastTypeCreateValidate createValidate) {
        FastType model = new FastType();
        model.setRelatedId(createValidate.getRelatedId());
        model.setName(createValidate.getName());
        model.setIcon(createValidate.getIcon());
        model.setPrice(createValidate.getPrice());
        model.setRemark(createValidate.getRemark());
        model.setExtend(createValidate.getExtend());
        model.setCode(createValidate.getCode());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        model.setGoldCoin(createValidate.getGoldCoin());
        fastTypeMapper.insert(model);
    }

    /**
     * 快递分类配置编辑
     *
     * @author DEREK
     * @param updateValidate 参数
     */
    @Override
    public void edit(FastTypeUpdateValidate updateValidate) {
        FastType model = fastTypeMapper.selectOne(
                new QueryWrapper<FastType>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setRelatedId(updateValidate.getRelatedId());
        model.setName(updateValidate.getName());
        model.setIcon(updateValidate.getIcon());
        model.setPrice(updateValidate.getPrice());
        model.setRemark(updateValidate.getRemark());
        model.setExtend(updateValidate.getExtend());
        model.setCode(updateValidate.getCode());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        model.setGoldCoin(updateValidate.getGoldCoin());
        fastTypeMapper.updateById(model);
    }

    /**
     * 快递分类配置删除
     *
     * @author DEREK
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        FastType model = fastTypeMapper.selectOne(
                new QueryWrapper<FastType>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        fastTypeMapper.delete(new QueryWrapper<FastType>().eq("id", id));
    }

}
